package proxy;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import pojo.Order;
import pojo.QueryVo;
import pojo.User;
import utils.SessionFactoryUtil;

import java.util.*;

public class proxyTest {

    @Test
    public void test() {
        SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口的代理实现类
        userMapper userMapper = sqlSession.getMapper(userMapper.class);
        QueryVo queryVo = new QueryVo();
        User user = new User();
        user.setUsername("张");
        queryVo.setUser(user);
        List<User> list = userMapper.getUserByQueryVo(queryVo);
        for(User user1 : list) {
            System.out.println(user1);
        }
        sqlSession.close();

    }
    @Test
    public void testCount() {
        SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口的代理实现类
        userMapper userMapper = sqlSession.getMapper(userMapper.class);
        System.out.println(userMapper.getUserById(28));
        sqlSession.close();
    }

    @Test
    public void testif() {
        SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口的代理实现类
        userMapper userMapper = sqlSession.getMapper(userMapper.class);
        User user = new User();
        user.setId(28);
        user.setUsername("ss");
        User user1 = userMapper.getUserByUserIdAndUsername(user);
        System.out.println(user1);
        sqlSession.close();
    }

    @Test
    public void testfor() {
        SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口的代理实现类
        userMapper userMapper = sqlSession.getMapper(userMapper.class);
        QueryVo queryVo = new QueryVo();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(40);
        ids.add(28);
        queryVo.setIds(ids);
        List<User> userList = userMapper.getUserList(queryVo);
        for(User user : userList) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    @Test
    public void testOne() {
        SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口的代理实现类
        userMapper userMapper = sqlSession.getMapper(userMapper.class);
        List<User> userList = userMapper.getUserOrders();
        for(User user : userList) {
            System.out.println(user);
            for(Order order : user.getOrders()) {
                System.out.print(" " + order);
            }
        }
        sqlSession.close();
    }
}
